package com.company.news.ui;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.company.news.R;
import com.company.news.common.MyActivity;
import com.company.news.common.MyLazyFragment;
import com.company.news.helper.ActivityStackManager;
import com.company.news.helper.DoubleClickHelper;
import com.company.news.ui.fragment.home.HomeFragment;
import com.company.news.ui.fragment.littlevideo.LittleVideoFragment;
import com.company.news.ui.fragment.micro.MicroFragment;
import com.company.news.ui.fragment.mine.MineFragment;
import com.company.news.ui.fragment.video.VideoFragment;
import com.hjq.base.BaseFragmentAdapter;

import butterknife.BindView;
import cn.jzvd.Jzvd;

public class MainActivity extends MyActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.vp_home_pager)
    ViewPager mViewPager;
    @BindView(R.id.bottom_bar)
    BottomBarLayout mBottomBarLayout;
    private BaseFragmentAdapter mPagerAdapter;
    private String tabIndex;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getTitleId() {
        return 0;
    }


    @Override
    protected void initView() {
        mViewPager.addOnPageChangeListener(this);

    }

    @Override
    protected void initData() {
        //添加fragment
        mPagerAdapter = new BaseFragmentAdapter(this);
        mPagerAdapter.addFragment(HomeFragment.newInstance());
        mPagerAdapter.addFragment(VideoFragment.newInstance());
        mPagerAdapter.addFragment(MicroFragment.newInstance());
        mPagerAdapter.addFragment(LittleVideoFragment.newInstance());
        mPagerAdapter.addFragment(MineFragment.newInstance());
        mViewPager.setAdapter(mPagerAdapter);
        // 限制页面数量
        mViewPager.setOffscreenPageLimit(mPagerAdapter.getCount());
        initListener();

    }

    private void initListener() {
        mBottomBarLayout.setViewPager(mViewPager);
        //设置条目点击的监听


        mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(BottomBarItem bottomBarItem, int position) {
//                setStatusBarColor(position);//设置状态栏颜色
                Jzvd.releaseAllVideos();//底部页签切换或者是下拉刷新，释放资源
                if (position == 0 || position == 1) {
                    //如果点击的是首页
                    Log.e("getCurrentItem===", mBottomBarLayout.getCurrentItem() + ""); //上一次的位置
                    Log.e("position===", position + "");    //当前选择的位置
                    if (mBottomBarLayout.getCurrentItem() == position) {
                        //如果当前页码和点击的页码一致,进行下拉刷新
                        String channelCode = "";
                        if (position == 0) {
//                            channelCode = ((HomeFragment) mFragments.get(0)).getCurrentChannelCode();//获取到首页当前显示的fragment的频道
                        } else {
//                            channelCode = ((VideoFragment) mFragments.get(1)).getCurrentChannelCode();//获取到视频当前显示的fragment的频道
                        }
//                        postTabRefreshEvent(bottomBarItem, position, channelCode);//发送下拉刷新的事件
                    }
                    return;
                }

                //如果点击了其他条目
                BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
                bottomItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换为原来的图标

//                cancelTabLoading(bottomItem);//停止旋转动画
            }
        });


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        Log.e("viewpager==position===", position + "");    //当前选择的位置

    }

    @Override
    public void onPageSelected(int position) {
        Jzvd.releaseAllVideos();//底部页签切换或者是下拉刷新，释放资源
        Log.e("viewpager==position===", position + "");    //当前选择的位置


    }


    /**
     * 是否在Fragment使用沉浸式
     */
    @Override
    public boolean isStatusBarEnabled() {
        return true;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 回调当前 Fragment 的 onKeyDown 方法
        MyLazyFragment currentFragment = (MyLazyFragment) mPagerAdapter.getCurrentFragment();
        if (currentFragment.onKeyDown(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (DoubleClickHelper.isOnDoubleClick()) {
            //移动到上一个任务栈，避免侧滑引起的不良反应
            moveTaskToBack(false);
            postDelayed(new Runnable() {

                @Override
                public void run() {
                    // 进行内存优化，销毁掉所有的界面
                    ActivityStackManager.getInstance().finishAllActivities();
                    if (Jzvd.backPress()) {
                        return;
                    }
                    // 销毁进程
                    System.exit(0);
                }
            }, 300);
        } else {
            toast(getResources().getString(R.string.home_exit_hint));
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    protected void onDestroy() {
        mViewPager.removeOnPageChangeListener(this);
        mViewPager.setAdapter(null);
        super.onDestroy();
    }

    @Override
    public boolean isSupportSwipeBack() {
        // 不使用侧滑功能
        return false;
    }


}
